/**
 * 				MochitoSocketServerCreator.java
 *				------------
 *		
 *   Author:	Moser Dominik
 *   ID:		bin04017
 *	 Created:	24.01.2007
 *	 Project:	Mochito-DataHandler
 *	 Package:	mochito.serverSide
 *	 Usage:		
 */
package mochito.serverSide;

import java.io.IOException;
import java.net.ServerSocket;

import mochito.tranferFactory.MochitoHost;

/**
 * @author bin04017
 *
 */
public class MochitoSocketServerCreator implements Runnable {

	private ServerSocket serverSocket = null;
	
    private boolean listening = true;

	private MochitoHost host = null;
    
	public MochitoSocketServerCreator(MochitoHost host){
		this.host  = host;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {


        try {
            serverSocket = new ServerSocket(host.getPort());
        } catch (IOException e) {
            System.err.println("Could not listen on port: "+host.getPort());
            System.exit(-1);
        }

        while (listening){
			try {
				new MochitoSockedServer(serverSocket.accept()).run();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
        while(listening){
        	try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
        }
        try {
			serverSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void stop(){
		listening = false;
	}

}
